package com.fengye.api.controller;

import com.fengye.common.annotation.Log;
import com.fengye.common.annotation.RateLimit;
import com.fengye.common.annotation.RequestLimit;
import com.fengye.common.controller.BaseController;
import com.fengye.common.domain.MyPage;
import com.fengye.common.domain.ResponseResult;
import com.fengye.common.domain.vo.ArticleListVo;
import com.fengye.common.domain.vo.ArticleDetailVo;
import com.fengye.common.domain.vo.HotArticleVo;
import com.fengye.api.service.ArticleService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * ArticleController
 *
 * @author 枫叶fy
 * @description 文章控制器
 * @since 2024/2/25 01:01
 */
@Api(tags = "文章模块")
@RestController
@RequestMapping("/article")
@CrossOrigin
public class ArticleController extends BaseController {
    @Resource
    private ArticleService articleService;

    /**
     * 查询热门文章
     * @return 热门文章列表
     */
    @RequestMapping(path = "/hotArticleList", method = {RequestMethod.GET})
    @RateLimit(key = "hotArticleList", permitsPerSecond = 100)
    @RequestLimit(maxCount = 3, interval = 5)
    public ResponseResult<List<HotArticleVo>> hotArticleList() {
        List<HotArticleVo> hotArticleVos = articleService.hotArticleList();
        return ResponseResult.success(hotArticleVos);
    }

    /**
     * 查询文章列表
     * @return 文章列表
     */
    @Log(businessName = "获取文章列表接口")
    @GetMapping(path = "/list")
    public ResponseResult<MyPage<ArticleListVo>> getArticleList(Long categoryId, Integer pageNum, Integer pageSize) {
        MyPage<ArticleListVo> articlePage = articleService.articleList(categoryId, pageNum, pageSize);
        return ResponseResult.success(articlePage);
    }

    /**
     * 查询文章列表
     * @return 文章列表
     */
    @GetMapping(path = "/{id}")
    public ResponseResult<ArticleDetailVo> getArticle(@PathVariable Long id) {
        ArticleDetailVo articleVo = articleService.getArticleDetail(id);
        return ResponseResult.success(articleVo);
    }

    /**
     * 更新文章浏览量
     * @param id 文章id
     * @return 响应结果
     */
    @PutMapping(path = "/updateViewCount/{id}")
    public ResponseResult<Void> updateViewCount(@PathVariable("id") Long id) {
        articleService.updateViewCount(id);
        return ResponseResult.success();
    }

}
